#include <cmath>
#include <cstdio>
#include <cstring>

const int N = 40005, M = 205;
int n, m, cnt[M], p[N], f[N], pre[N], buk[M][N];

inline void mmin(int &a, int b) {
  if (b < a) a = b;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("CppTest.in", "r", stdin);
  freopen("CppTest.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", &p[i]);
  memset(f, 0x3f, sizeof(f));
  f[0] = 0;
  m = ceil(sqrt(m));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j)
      if (!buk[j][p[i]]++) {
        ++cnt[j];
        if (cnt[j] > j) {
          do
            --buk[j][p[pre[j]]];
          while (buk[j][p[pre[j]++]] != 0);
          --cnt[j];
        }
      }
    for (int j = 1; j <= m; ++j) mmin(f[i], f[pre[j] - 1] + j * j);
  }
  printf("%d", f[n]);
  return 0;
}